home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_grp.py < prev    next >
Text File  |  2005-10-18  |  3KB  |  85 lines

  1. """Test script for the grp module."""
  2.  
  3. import grp
  4. import unittest
  5. from test import test_support
  6.  
  7. class GroupDatabaseTestCase(unittest.TestCase):
  8.  
  9.     def check_value(self, value):
  10.         # check that a grp tuple has the entries and
  11.         # attributes promised by the docs
  12.         self.assertEqual(len(value), 4)
  13.         self.assertEqual(value[0], value.gr_name)
  14.         self.assert_(isinstance(value.gr_name, basestring))
  15.         self.assertEqual(value[1], value.gr_passwd)
  16.         self.assert_(isinstance(value.gr_passwd, basestring))
  17.         self.assertEqual(value[2], value.gr_gid)
  18.         self.assert_(isinstance(value.gr_gid, int))
  19.         self.assertEqual(value[3], value.gr_mem)
  20.         self.assert_(isinstance(value.gr_mem, list))
  21.  
  22.     def test_values(self):
  23.         entries = grp.getgrall()
  24.  
  25.         for e in entries:
  26.             self.check_value(e)
  27.  
  28.         for e in entries:
  29.             e2 = grp.getgrgid(e.gr_gid)
  30.             self.check_value(e2)
  31.             self.assertEqual(e2.gr_gid, e.gr_gid)
  32.             e2 = grp.getgrnam(e.gr_name)
  33.             self.check_value(e2)
  34.             self.assertEqual(e2.gr_name, e.gr_name)
  35.  
  36.     def test_errors(self):
  37.         self.assertRaises(TypeError, grp.getgrgid)
  38.         self.assertRaises(TypeError, grp.getgrnam)
  39.         self.assertRaises(TypeError, grp.getgrall, 42)
  40.  
  41.         # try to get some errors
  42.         bynames = {}
  43.         bygids = {}
  44.         for (n, p, g, mem) in grp.getgrall():
  45.             bynames[n] = g
  46.             bygids[g] = n
  47.  
  48.         allnames = bynames.keys()
  49.         namei = 0
  50.         fakename = allnames[namei]
  51.         while fakename in bynames:
  52.             chars = map(None, fakename)
  53.             for i in xrange(len(chars)):
  54.                 if chars[i] == 'z':
  55.                     chars[i] = 'A'
  56.                     break
  57.                 elif chars[i] == 'Z':
  58.                     continue
  59.                 else:
  60.                     chars[i] = chr(ord(chars[i]) + 1)
  61.                     break
  62.             else:
  63.                 namei = namei + 1
  64.                 try:
  65.                     fakename = allnames[namei]
  66.                 except IndexError:
  67.                     # should never happen... if so, just forget it
  68.                     break
  69.             fakename = ''.join(map(None, chars))
  70.  
  71.         self.assertRaises(KeyError, grp.getgrnam, fakename)
  72.  
  73.         # Choose a non-existent gid.
  74.         fakegid = 4127
  75.         while fakegid in bygids:
  76.             fakegid = (fakegid * 3) % 0x10000
  77.  
  78.         self.assertRaises(KeyError, grp.getgrgid, fakegid)
  79.  
  80. def test_main():
  81.     test_support.run_unittest(GroupDatabaseTestCase)
  82.  
  83. if __name__ == "__main__":
  84.     test_main()
  85.